package com.lehecai.core.lottery.content.util;

import com.lehecai.core.exception.lottery.content.UnmatchedElementAttributeClassException;
import com.lehecai.core.exception.lottery.content.UnsupportedElementAttributeException;
import com.lehecai.core.lottery.content.IElementAttributeParser;
import com.lehecai.core.lottery.content.impl.LotteryTypeSpecifiedElementAttribute;
import com.lehecai.core.lottery.content.impl.LotteryTypeSpecifiedElementAttributeParser;
import com.lehecai.core.lottery.plan.PlanContentElement;
import com.lehecai.core.lottery.ticket.TicketContentElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: sunshow
 */
public class LotteryTypeSpecifiedElementParser {

    protected static IElementAttributeParser elementAttributeParser = new LotteryTypeSpecifiedElementAttributeParser();

    public static LotteryTypeSpecifiedElementResult parse(PlanContentElement[] planContentElementArray) throws UnsupportedElementAttributeException, UnmatchedElementAttributeClassException  {
        Map<String, List<LotteryTypeSpecifiedElementAttribute>> elementMap = new HashMap<String, List<LotteryTypeSpecifiedElementAttribute>>();

        for (PlanContentElement contentElement : planContentElementArray) {
            String element = contentElement.getElement();
            String[] attributes = contentElement.getAttributes();

            List<LotteryTypeSpecifiedElementAttribute> attributeList = parseAttribute(attributes);

            elementMap.put(element, attributeList);
        }
        return new LotteryTypeSpecifiedElementResult(elementMap);
    }

    public static LotteryTypeSpecifiedElementResult parse(TicketContentElement[] ticketContentElementArray) throws UnsupportedElementAttributeException, UnmatchedElementAttributeClassException  {
        Map<String, List<LotteryTypeSpecifiedElementAttribute>> elementMap = new HashMap<String, List<LotteryTypeSpecifiedElementAttribute>>();

        for (TicketContentElement contentElement : ticketContentElementArray) {
            String element = contentElement.getElement();
            String[] attributes = contentElement.getAttributes();

            List<LotteryTypeSpecifiedElementAttribute> attributeList = parseAttribute(attributes);

            elementMap.put(element, attributeList);
        }
        return new LotteryTypeSpecifiedElementResult(elementMap);
    }

    public static List<LotteryTypeSpecifiedElementAttribute> parseAttribute(String[] attributes) throws UnsupportedElementAttributeException, UnmatchedElementAttributeClassException {
        List<LotteryTypeSpecifiedElementAttribute> attributeList = new ArrayList<LotteryTypeSpecifiedElementAttribute>();

        for (String attribute : attributes) {
            LotteryTypeSpecifiedElementAttribute elementAttribute = elementAttributeParser.parse(attribute, LotteryTypeSpecifiedElementAttribute.class);
            attributeList.add(elementAttribute);
        }

        return attributeList;
    }
}
